package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public List<NursingLevelVo> selectAll() {
        return nursingLevelMapper.selectAll();
    }

    @Override
    public int addNursingLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class);

        nursingLevel.setStatus(1);

        int row= nursingLevelMapper.countByname(nursingLevel.getName());
        if (row>0){
            throw new BaseException(BasicEnum.UPDATE_NURSINGLEVEL_FAIL);
        }
        row=nursingLevelMapper.countByPlanId(nursingLevel.getPlanId());
        if(row>0){
            throw new BaseException(BasicEnum.UPDATE_NURSING_LEVEL_FAIL);
        }
        return nursingLevelMapper.insert(nursingLevel);
    }

    @Override
    public PageResponse<NursingLevelVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevelVo> page = nursingLevelMapper.selectPageByNameAndStatus(name, status);


        return PageResponse.of(page, NursingLevelVo.class);
    }

    @Override
    public NursingLevelVo selectById(Long id) {
        return nursingLevelMapper.selectById(id);
    }

    @Override
    public int updateNursingLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class);
        NursingLevelVo nursingLevelVo = selectById(nursingLevel.getId());
        String oldName = nursingLevelVo.getName();
        String name = nursingLevel.getName();
        if (!name.equals(oldName)) {
            int row = nursingLevelMapper.countByname(name);
            if (row > 0) {
                throw new BaseException(BasicEnum.UPDATE_NURSINGLEVEL_FAIL);
            }
        }
        Long oldPlanId = nursingLevelVo.getPlanId();
        Long planId = nursingLevel.getPlanId();
        if(oldPlanId!=planId){
            int row=nursingLevelMapper.countByPlanId(nursingLevel.getPlanId());
            if(row>0){
                throw new BaseException(BasicEnum.UPDATE_NURSING_LEVEL_FAIL);
            }
        }


        return nursingLevelMapper.update(nursingLevel);
    }

    @Override
    public int deleteById(Long id) {
        return nursingLevelMapper.delete(id);
    }

    @Override
    public int updateNursingLevelStatus(Long id, Integer status) {
        return nursingLevelMapper.updateStatus(id,status);
    }
}
